package 算法.leetcode.offer;

import 算法.leetcode.offer.base.TreeNode;

import java.util.ArrayList;
import java.util.List;

/**
 * @author lchenglong
 * @date 2022/3/19
 */
public class Offer54 {

    public static void main(String[] args) {
        Offer54 instance = new Offer54();
        // 3,1,4,null,2
        List<Integer> list = new ArrayList<Integer>();
        list.add(3);
        list.add(2);
        list.add(4);
        list.add(1);
        TreeNode root = TreeNode.init(list);
        int ans = instance.kthLargest(root, 3);
        System.out.println(ans);
    }
    int k,res;
    public int kthLargest(TreeNode root, int k) {
        this.k = k;
        addChild(root);
        return res;
    }

    public void addChild(TreeNode root){
        if (root == null) return;
        addChild(root.right);
        if (k == 0)return;
        if (--k == 0)res = root.val;
        addChild(root.left);
    }
}
